package fr.ajicommerce;

import java.util.Map;

import javax.faces.context.FacesContext;

import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.config.Scope;

public class CustomRechercheViewScope implements Scope{
	
    public Object get(String name, ObjectFactory<?> objectFactory) {
        Map<String,Object> viewMap = FacesContext.getCurrentInstance().getViewRoot().getViewMap();
        
        if(viewMap.containsKey(name)) {
       	
            return viewMap.get(name);
        }
        else 
        {
        	System.out.println(name);
        	FacesContext context = FacesContext.getCurrentInstance();
        	Object object = null;
        	if(context.getExternalContext().getSessionMap().get("rechercheBean") != null)
        	{
        		object = context.getExternalContext().getSessionMap().get("rechercheBean");
        		context.getExternalContext().getSessionMap().remove("rechercheBean");
        	}
        	else
        		object = objectFactory.getObject();
            
        	viewMap.put(name, object);
            return object;
    	}
    }
    
 
    public Object remove(String name) {
		
        return FacesContext.getCurrentInstance().getViewRoot().getViewMap().remove(name);
    }
 
    public String getConversationId() {
        return null;
    }
 
    public void registerDestructionCallback(String name, Runnable callback) {
        //Not supported
    }
 
    public Object resolveContextualObject(String key) {
        return null;
    }


}
